home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 2003 August / MW 8 2003 CD1.iso / Inside Macworld / Product News / gimp-1.2.4.sit / gimp-1.2.4 / plug-ins / common / borderaverage.c < prev    next >
Encoding:
C/C++ Source or Header  |  2003-01-15  |  12.1 KB  |  471 lines

  1. /* borderaverage 0.01 - image processing plug-in for the Gimp 1.0 API
  2.  *
  3.  * Copyright (C) 1998 Philipp Klaus (webmaster@access.ch)
  4.  *
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * (at your option) any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software
  18.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19.  */
  20. #include "config.h"
  21.  
  22. #include <stdlib.h>
  23.  
  24. #include <gtk/gtk.h>
  25.  
  26. #include <libgimp/gimp.h>
  27. #include <libgimp/gimpui.h>
  28.  
  29. #include "libgimp/stdplugins-intl.h"
  30.  
  31.  
  32. /* Declare local functions.
  33.  */
  34. static void      query  (void);
  35. static void      run    (gchar     *name,
  36.              gint       nparams,
  37.              GimpParam    *param,
  38.              gint      *nreturn_vals,
  39.              GimpParam   **return_vals);
  40.  
  41. static void      borderaverage (GimpDrawable *drawable,
  42.                 guchar    *res_r,
  43.                 guchar    *res_g,
  44.                 guchar    *res_b);
  45.  
  46. static gint      borderaverage_dialog (void);
  47.  
  48. static void      add_new_color (gint    bytes,
  49.                 guchar *buffer,
  50.                 gint   *cube,
  51.                 gint    bucket_expo);
  52.  
  53. GimpPlugInInfo PLUG_IN_INFO =
  54. {
  55.   NULL,  /* init  */
  56.   NULL,  /* quit  */
  57.   query, /* query */
  58.   run,   /* run   */
  59. };
  60.  
  61. static gint  borderaverage_thickness       = 3;
  62. static gint  borderaverage_bucket_exponent = 4;
  63.  
  64. struct borderaverage_data
  65. {
  66.   gint  thickness;
  67.   gint  bucket_exponent;
  68. }
  69. borderaverage_data =
  70. {
  71.   3,
  72.   4
  73. };
  74.  
  75.  
  76. MAIN ()
  77.  
  78. static void
  79. query (void)
  80. {
  81.   static GimpParamDef args[] =
  82.   {
  83.     { GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" },
  84.     { GIMP_PDB_IMAGE, "image", "Input image (unused)" },
  85.     { GIMP_PDB_DRAWABLE, "drawable", "Input drawable" },
  86.     { GIMP_PDB_INT32, "thickness", "Border size to take in count" },
  87.     { GIMP_PDB_INT32, "bucket_exponent", "Bits for bucket size (default=4: 16 Levels)" },
  88.   };
  89.   static GimpParamDef return_vals[] = 
  90.   {
  91.     { GIMP_PDB_INT32, "num_channels", "Number of color channels returned (always 3)" },
  92.     { GIMP_PDB_INT8ARRAY, "color_vals", "The average color of the specified border"},
  93.   };
  94.   static int nargs = sizeof (args) / sizeof (args[0]);
  95.   static int nreturn_vals = sizeof (return_vals) / sizeof (return_vals[0]);
  96.  
  97.   INIT_I18N();
  98.  
  99.   gimp_install_procedure ("plug_in_borderaverage",
  100.               "Borderaverage",
  101.               "",
  102.               "Philipp Klaus",
  103.               "Internet Access AG",
  104.               "1998",
  105.               N_("<Image>/Filters/Colors/Border Average..."),
  106.               "RGB*",
  107.               GIMP_PLUGIN,
  108.               nargs, nreturn_vals,
  109.               args, return_vals);
  110. }
  111.  
  112. static void
  113. run (gchar   *name,
  114.      gint     nparams,
  115.      GimpParam  *param,
  116.      gint    *nreturn_vals,
  117.      GimpParam **return_vals)
  118. {
  119.   static GimpParam values[3];
  120.   GimpDrawable *drawable;
  121.   GimpRunModeType run_mode;
  122.   GimpPDBStatusType status = GIMP_PDB_SUCCESS;
  123.   gint8        *result_color;
  124.  
  125.   INIT_I18N_UI();
  126.  
  127.   run_mode = param[0].data.d_int32;
  128.     
  129.   /* get the return memory */
  130.   result_color = (gint8 *) g_new(gint8, 3);
  131.  
  132.   /*    Get the specified drawable    */
  133.   drawable = gimp_drawable_get (param[2].data.d_drawable);
  134.  
  135.   switch (run_mode)
  136.     {
  137.     case GIMP_RUN_INTERACTIVE:
  138.       gimp_get_data ("plug_in_borderaverage", &borderaverage_data);
  139.       borderaverage_thickness       = borderaverage_data.thickness;
  140.       borderaverage_bucket_exponent = borderaverage_data.bucket_exponent;
  141.       if (! borderaverage_dialog ())
  142.     status = GIMP_PDB_EXECUTION_ERROR;
  143.       break;
  144.  
  145.     case GIMP_RUN_NONINTERACTIVE:
  146.       if (nparams != 5)
  147.     status = GIMP_PDB_CALLING_ERROR;
  148.       if (status == GIMP_PDB_SUCCESS)
  149.     {
  150.       borderaverage_thickness       = param[3].data.d_int32;
  151.       borderaverage_bucket_exponent = param[4].data.d_int32;
  152.     }
  153.       break;
  154.  
  155.     case GIMP_RUN_WITH_LAST_VALS:
  156.       gimp_get_data ("plug_in_borderaverage", &borderaverage_data);
  157.       borderaverage_thickness       = borderaverage_data.thickness;
  158.       borderaverage_bucket_exponent = borderaverage_data.bucket_exponent;
  159.       break;
  160.  
  161.     default:
  162.       break;
  163.     }
  164.  
  165.   if (status == GIMP_PDB_SUCCESS)
  166.     {
  167.       /*  Make sure that the drawable is RGB color  */
  168.       if (gimp_drawable_is_rgb (drawable->id))
  169.     {
  170.       gimp_progress_init ( _("Border Average..."));
  171.       borderaverage (drawable,
  172.              &result_color[0], &result_color[1], &result_color[2]);
  173.  
  174.       if (run_mode != GIMP_RUN_NONINTERACTIVE)
  175.         {
  176.           gimp_palette_set_foreground (result_color[0],
  177.                        result_color[1],
  178.                        result_color[2]);
  179.         }
  180.       if (run_mode == GIMP_RUN_INTERACTIVE)
  181.         {
  182.           borderaverage_data.thickness       = borderaverage_thickness;
  183.           borderaverage_data.bucket_exponent = borderaverage_bucket_exponent;
  184.           gimp_set_data ("plug_in_borderaverage",
  185.                  &borderaverage_data, sizeof (borderaverage_data));
  186.         }
  187.     }
  188.       else
  189.     {
  190.       status = GIMP_PDB_EXECUTION_ERROR;
  191.     }
  192.     }
  193.   *nreturn_vals = 3;
  194.   *return_vals = values;
  195.  
  196.   values[0].type = GIMP_PDB_STATUS;
  197.   values[0].data.d_status = status;
  198.     
  199.   values[1].type = GIMP_PDB_INT32;
  200.   values[1].data.d_int32 = 3;
  201.     
  202.   values[2].type = GIMP_PDB_INT8ARRAY;
  203.   values[2].data.d_int8array = result_color;
  204.  
  205.   gimp_drawable_detach (drawable);
  206. }
  207.  
  208. static void
  209. borderaverage (GimpDrawable *drawable, 
  210.            guchar    *res_r, 
  211.            guchar    *res_g, 
  212.            guchar    *res_b) 
  213. {
  214.   gint         width;
  215.   gint         height;
  216.   gint         x1, x2, y1, y2;
  217.   gint         bytes;
  218.   gint         max;
  219.  
  220.   guchar       r, g, b;
  221.  
  222.   guchar      *buffer;
  223.   gint         bucket_num, bucket_expo, bucket_rexpo;
  224.  
  225.   gint        *cube;
  226.  
  227.   gint         row, col, i,j,k; /* index variables */
  228.  
  229.   GimpPixelRgn    myPR;
  230.  
  231.   /* allocate and clear the cube before */
  232.   bucket_expo = borderaverage_bucket_exponent;
  233.   bucket_rexpo = 8 - bucket_expo;
  234.   cube = g_new (gint, 1 << (bucket_rexpo * 3));
  235.   bucket_num = 1 << bucket_rexpo;
  236.         
  237.   for (i = 0; i < bucket_num; i++)
  238.     {
  239.       for (j = 0; j < bucket_num; j++)
  240.     {
  241.       for (k = 0; k < bucket_num; k++)
  242.         {
  243.           cube[(i << (bucket_rexpo << 1)) + (j << bucket_rexpo) + k] = 0;
  244.         }
  245.     }
  246.     }
  247.  
  248.   /*  Get the input area. This is the bounding box of the selection in
  249.    *  the image (or the entire image if there is no selection). Only
  250.    *  operating on the input area is simply an optimization. It doesn't
  251.    *  need to be done for correct operation. (It simply makes it go
  252.    *  faster, since fewer pixels need to be operated on).
  253.    */
  254.   gimp_drawable_mask_bounds (drawable->id, &x1, &y1, &x2, &y2);
  255.  
  256.   /*  Get the size of the input image. (This will/must be the same
  257.    *  as the size of the output image.
  258.    */
  259.   width = drawable->width;
  260.   height = drawable->height;
  261.   bytes = drawable->bpp;
  262.  
  263.   gimp_tile_cache_ntiles (2 * ((x2 - x1) / gimp_tile_width () + 1));
  264.  
  265.   /*  allocate row buffer  */
  266.   buffer = g_new (guchar, (x2 - x1) * bytes);
  267.  
  268.   /*  initialize the pixel regions  */
  269.   gimp_pixel_rgn_init (&myPR, drawable, 0, 0, width, height, FALSE, FALSE);
  270.  
  271.   /*  loop through the rows, performing our magic*/
  272.   for (row = y1; row < y2; row++)
  273.     {
  274.       gimp_pixel_rgn_get_row (&myPR, buffer, x1, row, (x2-x1));
  275.     
  276.       if (row < y1 + borderaverage_thickness ||
  277.       row >= y2 - borderaverage_thickness)
  278.     {
  279.       /* add the whole row */
  280.       for (col = 0; col < ((x2 - x1) * bytes); col += bytes)
  281.         {
  282.           add_new_color (bytes, &buffer[col], cube, bucket_expo);
  283.         }
  284.     }
  285.       else
  286.     {
  287.       /* add the left border */
  288.       for (col = 0; col < (borderaverage_thickness * bytes); col += bytes)
  289.         {
  290.           add_new_color (bytes, &buffer[col], cube, bucket_expo);
  291.         }
  292.       /* add the right border */
  293.       for (col = ((x2 - x1 - borderaverage_thickness) * bytes);
  294.            col < ((x2 - x1) * bytes); col += bytes)
  295.         {
  296.           add_new_color (bytes, &buffer[col], cube, bucket_expo);
  297.         }
  298.     }
  299.  
  300.       if ((row % 5) == 0)
  301.     gimp_progress_update ((double) row / (double) (y2 - y1));
  302.     }
  303.  
  304.   max = 0; r = 0; g = 0; b = 0;
  305.  
  306.   /* get max of cube */
  307.   for (i = 0; i < bucket_num; i++)
  308.     {
  309.       for (j = 0; j < bucket_num; j++)
  310.     {
  311.       for (k = 0; k < bucket_num; k++)
  312.         {
  313.           if (cube[(i << (bucket_rexpo << 1)) +
  314.               (j << bucket_rexpo) + k] > max)
  315.         {
  316.           max = cube[(i << (bucket_rexpo << 1)) +
  317.                 (j << bucket_rexpo) + k];
  318.           r = (i<<bucket_expo) + (1<<(bucket_expo - 1));
  319.           g = (j<<bucket_expo) + (1<<(bucket_expo - 1));
  320.           b = (k<<bucket_expo) + (1<<(bucket_expo - 1));
  321.         }
  322.         }
  323.     }
  324.     }
  325.  
  326.   /* return the color */
  327.   *res_r = r;
  328.   *res_g = g;
  329.   *res_b = b;
  330.  
  331.   g_free (buffer);
  332.   g_free (cube);
  333. }
  334.  
  335. static void 
  336. add_new_color (gint    bytes, 
  337.            guchar *buffer, 
  338.            gint   *cube, 
  339.            gint    bucket_expo) 
  340. {
  341.   guchar r, g, b;
  342.   gint     bucket_rexpo;
  343.  
  344.   bucket_rexpo = 8 - bucket_expo;
  345.   r = buffer[0] >>bucket_expo;
  346.   if (bytes > 1)
  347.     {
  348.       g = buffer[1] >>bucket_expo;
  349.     }
  350.   else
  351.     {
  352.       g = 0;
  353.     }
  354.   if (bytes > 2)
  355.     {
  356.       b = buffer[2] >>bucket_expo;
  357.     }
  358.   else
  359.     {
  360.       b = 0;
  361.     }
  362.   cube[(r << (bucket_rexpo << 1)) + (g << bucket_rexpo) + b]++;
  363. }
  364.  
  365. static gboolean run_flag = FALSE;
  366.  
  367. static void
  368. borderaverage_ok_callback (GtkWidget *widget,
  369.                gpointer   data)
  370. {
  371.   run_flag = TRUE;
  372.  
  373.   gtk_widget_destroy (GTK_WIDGET (data));
  374. }
  375.  
  376. static gint 
  377. borderaverage_dialog (void)
  378. {
  379.   GtkWidget *dlg;
  380.   GtkWidget *frame;
  381.   GtkWidget *vbox;
  382.   GtkWidget *hbox;
  383.   GtkWidget *label;
  384.   GtkWidget *spinbutton;
  385.   GtkObject *adj;
  386.   GtkWidget *menu;
  387.  
  388.   gimp_ui_init ("borderaverage", FALSE);
  389.  
  390.   dlg = gimp_dialog_new (_("Borderaverage"), "borderaverage",
  391.              gimp_standard_help_func, "filters/borderaverage.html",
  392.              GTK_WIN_POS_MOUSE,
  393.              FALSE, TRUE, FALSE,
  394.  
  395.              _("OK"), borderaverage_ok_callback,
  396.              NULL, NULL, NULL, TRUE, FALSE,
  397.              _("Cancel"), gtk_widget_destroy,
  398.              NULL, 1, NULL, FALSE, TRUE,
  399.  
  400.              NULL);
  401.  
  402.   gtk_signal_connect (GTK_OBJECT (dlg), "destroy",
  403.               GTK_SIGNAL_FUNC (gtk_main_quit),
  404.               NULL);
  405.  
  406.   vbox = gtk_vbox_new (FALSE, 4);
  407.   gtk_container_set_border_width (GTK_CONTAINER (vbox), 6);
  408.   gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dlg)->vbox), vbox, TRUE, TRUE, 0);
  409.   gtk_widget_show (vbox);
  410.  
  411.   frame = gtk_frame_new (_("Border Size"));
  412.   gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
  413.   gtk_widget_show (frame);
  414.  
  415.   hbox = gtk_hbox_new (FALSE, 4);
  416.   gtk_container_set_border_width (GTK_CONTAINER (hbox), 4);
  417.   gtk_container_add (GTK_CONTAINER (frame), hbox);
  418.   gtk_widget_show (hbox);
  419.  
  420.   label = gtk_label_new (_("Thickness:"));
  421.   gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  422.   gtk_widget_show (label);
  423.  
  424.   spinbutton = gimp_spin_button_new (&adj, borderaverage_thickness,
  425.                      0, 256, 1, 5, 0, 0, 0);
  426.   gtk_box_pack_start (GTK_BOX (hbox), spinbutton, FALSE, FALSE, 0);
  427.   gtk_widget_show (spinbutton);
  428.  
  429.   gtk_signal_connect (GTK_OBJECT (adj), "value_changed",
  430.               GTK_SIGNAL_FUNC (gimp_int_adjustment_update),
  431.               &borderaverage_thickness);
  432.  
  433.   frame = gtk_frame_new (_("Number of Colors"));
  434.   gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
  435.   gtk_widget_show (frame);
  436.  
  437.   hbox = gtk_hbox_new (FALSE, 4);
  438.   gtk_container_set_border_width (GTK_CONTAINER (hbox), 4);
  439.   gtk_container_add (GTK_CONTAINER (frame), hbox);
  440.   gtk_widget_show (hbox);
  441.  
  442.   label = gtk_label_new (_("Bucket Size:"));
  443.   gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  444.   gtk_widget_show (label);
  445.  
  446.   menu = gimp_option_menu_new2 (FALSE, gimp_menu_item_update,
  447.                 &borderaverage_bucket_exponent,
  448.                 GINT_TO_POINTER (borderaverage_bucket_exponent),
  449.  
  450.                 _("1 (nonsense?)"),   GINT_TO_POINTER (0), NULL,
  451.                 "2",                  GINT_TO_POINTER (1), NULL,
  452.                 "4",                  GINT_TO_POINTER (2), NULL,
  453.                 "8",                  GINT_TO_POINTER (3), NULL,
  454.                 "16",                 GINT_TO_POINTER (4), NULL,
  455.                 "32",                 GINT_TO_POINTER (5), NULL,
  456.                 "64",                 GINT_TO_POINTER (6), NULL,
  457.                 "128",                GINT_TO_POINTER (7), NULL,
  458.                 _("256 (nonsense?)"), GINT_TO_POINTER (8), NULL,
  459.  
  460.                 NULL);
  461.   gtk_box_pack_start (GTK_BOX (hbox), menu, FALSE, FALSE, 0);
  462.   gtk_widget_show (menu);
  463.  
  464.   gtk_widget_show (dlg);
  465.  
  466.   gtk_main ();
  467.   gdk_flush ();
  468.  
  469.   return run_flag;
  470. }
  471.